//index.js
//获取应用实例
const utils = require('../../utils/util.js');
const app = getApp();
Page({
    data: {
        count: 0,
        pindex: 1,
        param:"",
        indicatorDots: true,
        autoplay: true,
        interval: 3000,
        duration: 500,
        circular: true,
        //中间的选择器
        product_recommend: [
            {
                name: '全部订单',
                id: 1,
                selected: 'selected'
            },
            {
                name: '待付款',
                id: 2,
                selected: ''
            },
            {
                name: '待发货',
                id: 3,
                selected: ''
            },
            {
                name: '待收货',
                id: 4,
                selected: ''
            }
        ],
        product_list: [],
    },
    bannelclick: function (e) {
        console.log(e);
        wx.navigateTo({
            url: '/pages/index/special?id=' + e.currentTarget.dataset.id,
        })
    },

    onLoad: function () {

        wx.showLoading({
            title: '玩命加载中',
        });
        var $this = this;
        //需要去请求数据
        utils.geturl({
            "handler": "Home",
            "method":"orderList",
            "param":'{"act":"all","p":"1"}',
            "token":app.globalData.token
        }, function (res) {
            $this.setData({
                "product_list": res.data.list
            });
        });
    },



    buy_now: function (e) {
        // wx.switchTab({
        //     url: '../index/index'
        // });

        var $this = this;

        console.log(e);
        console.log('id = '+ $this.data.id);
        console.log('price = '+ $this.data.price);

        var $this = this;
        utils.geturl({
            "handler": "Pay",
            "method": "wechatPay",
            "param": '{"id":"' + e.target.dataset.id + '","price":"' + e.target.dataset.price + '","number":"1","order_no":"'+e.target.dataset.order_no+'"}',
            "token": app.globalData.token
        }, function (res) {
            if (res.code==200) {

                wx.requestPayment({
                    'timeStamp': res.data.obj.callback.timeStamp,
                    'nonceStr': res.data.obj.callback.nonceStr,
                    'package': res.data.obj.callback.package,
                    'signType': 'MD5',
                    'paySign': res.data.obj.callback.paySign,
                    'success':function(res){
                        wx.showToast({
                            title: '支付成功'
                        });
                    },
                    'fail':function(res){
                        wx.showToast({
                            title: '支付失败',
                            icon: "none"
                        });
                    }
                });
                wx.showToast({
                    title: res.error_msg
                });
                $this.setData({
                    "coll": res.data.obj.coll
                });
            }
        });


    },



    //推荐点击
    recclick: function (e) {
        var $this = this;
        for (var item in this.data.product_recommend) {

            if (this.data.product_recommend[item].id == e.target.dataset.id && this.data.product_recommend[item].selected == "") {
                this.data.product_recommend[item].selected = 'selected';

                if (e.target.dataset.id == 1) {
                    $this.data.param = '{"act":"all","p":"1"}';
                    $this.data.pindex = 1;
                } else if (e.target.dataset.id == 2) {
                    $this.data.param = '{"act":"not_pay","p":"1"}';
                    $this.data.pindex = 1;
                } else if (e.target.dataset.id == 3) {
                    $this.data.param = '{"act":"not_put","p":"1"}';
                    $this.data.pindex = 1;
                }else if (e.target.dataset.id == 4) {
                    $this.data.param = '{"act":"not_get","p":"1"}';
                    $this.data.pindex = 1;
                }

            } else {
                this.data.product_recommend[item].selected = '';
            }
        }

        // console.log(this.data.product_recommend);
        // Page.prototype.setData();
        this.setData({
            "product_recommend": this.data.product_recommend
        });
        console.log($this.data.act);
        if ($this.data.act != "") {
            //需要去请求数据
            utils.geturl({
                "handler": "Home",
                "method":"orderList",
                "param": $this.data.param,
                "token":app.globalData.token
            }, function (res) {
                console.log(res)
                if (res.code == 200) {
                    $this.setData({
                        "product_list": res.data.list
                    });
                } else {
                    wx.showToast({
                        title: '服务器错误,请稍候再试',
                        icon: "none"
                    });
                }
            });
        }
    },


    productclick: function () {
        console.log(Math.random());
    }

})